package com.tt.job.util;

import com.tt.job.JobInfo;
import com.tt.job.api.schedule.JobSchedule;
import com.tt.job.api.schedule.async.AsyncJobSchedule;
import com.tt.job.api.schedule.event.EventJobSchedule;
import com.tt.job.api.schedule.triger.TriggerJobSchedule;
import com.tt.job.api.type.JobType;
import com.tt.job.common.util.JsonUtil;

public class JobUtils {

    public static JobInfo getJobInstance(String jobType, String jobClass, byte[] data) {
        JobInfo info;
        if (data.length != 0) {
            info = JsonUtil.decode(data, JobInfo.class, "UTF-8");
            if (info != null) {
                info.setJobClass(jobClass);
                info.setType(JobType.valueOf(jobType.toUpperCase()));
            }
            return info;
        } else {
            return null;
        }
    }

    public static JobSchedule getScheduleInstance(String type, byte[] data) {
        switch (type.toLowerCase()) {
            case "trigger":
                return JsonUtil.decode(data, TriggerJobSchedule.class);
            case "event":
                return JsonUtil.decode(data, EventJobSchedule.class);
            case "async":
                return JsonUtil.decode(data, AsyncJobSchedule.class);
        }
        return null;
    }
}
